package cn.tedu.bece.admin.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import cn.tedu.bece.admin.common.annotation.Log;
import cn.tedu.bece.admin.common.response.JsonResult;
import cn.tedu.bece.admin.common.response.PageObject;
import cn.tedu.bece.admin.system.pojo.dto.SysUserAddParam;
import cn.tedu.bece.admin.system.pojo.dto.SysUserQuery;
import cn.tedu.bece.admin.system.pojo.dto.SysUserUpdateParam;
import cn.tedu.bece.admin.system.pojo.entity.SysUser;
import cn.tedu.bece.admin.system.pojo.vo.SysUserDetailVO;
import cn.tedu.bece.admin.system.service.ISysUserService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Api(tags = "30.用户管理")
@RestController
@RequestMapping("/api/sys/users")
public class SysUserController {

    @Autowired
    private ISysUserService sysUserService;

    @Log(value = "新增用户")
    @ApiOperation(value = "新增用户")
    @ApiOperationSupport(order = 100)
    @SaCheckPermission("sys:user:add")
    @PostMapping
    public JsonResult add(@Validated @RequestBody SysUserAddParam sysUserAddParam) {
        sysUserService.insert(sysUserAddParam);
        return JsonResult.success();
    }

    @Log(value = "删除用户")
    @ApiOperation(value = "删除用户")
    @ApiOperationSupport(order = 200)
    @SaCheckPermission("sys:user:del")
    @DeleteMapping
    public JsonResult delete(@RequestParam("ids") List<Long> ids) {
        sysUserService.deleteByIds(ids);
        return JsonResult.success();
    }

    @Log(value = "修改用户")
    @ApiOperation(value = "修改用户")
    @ApiOperationSupport(order = 300)
    @SaCheckPermission("sys:user:edit")
    @PutMapping
    public JsonResult update(@Validated @RequestBody SysUserUpdateParam sysUserUpdateParam) {
        sysUserService.updateById(sysUserUpdateParam);
        return JsonResult.success();
    }

    @Log(value = "修改状态")
    @ApiOperation(value = "修改状态")
    @ApiOperationSupport(order = 310)
    @SaCheckPermission("sys:user:edit")
    @PatchMapping("/{id}/status")
    public JsonResult updateStatus(@PathVariable("id") Long id, @RequestParam("status") Integer status) {
        sysUserService.updateStatus(id, status);
        return JsonResult.success();
    }

    @Log(value = "重置密码")
    @ApiOperation(value = "重置密码")
    @ApiOperationSupport(order = 320)
    @SaCheckPermission("sys:user:edit")
    @PatchMapping("/{id}/resetPassword")
    public JsonResult resetPassword(@PathVariable("id") Long id) {
        sysUserService.resetPassword(id);
        return JsonResult.success();
    }

    @ApiOperation(value = "查询用户详情")
    @ApiOperationSupport(order = 400)
    @SaCheckPermission("sys:user:query")
    @GetMapping("/{id}")
    public JsonResult getDetail(@PathVariable("id") Long id) {
        SysUserDetailVO sysUserDetailVO = sysUserService.selectById(id);
        return JsonResult.success(sysUserDetailVO);
    }

    @ApiOperation(value = "查询用户列表")
    @ApiOperationSupport(order = 410)
    @SaCheckPermission("sys:user:list")
    @GetMapping
    public JsonResult list(Integer pageNum, Integer pageSize, SysUserQuery query) {
        PageObject<SysUser> pageObject = PageObject.page(pageNum, pageSize, () -> sysUserService.selectList(query));
        return JsonResult.success(pageObject);
    }

    @ApiOperation(value = "导出用户列表")
    @ApiOperationSupport(order = 500)
    @SaCheckPermission("sys:user:export")
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        List<SysUser> rows = sysUserService.selectListForExport();
        // 创建ExcelWriter
        ExcelWriter writer = ExcelUtil.getWriter();
        // 自定义标题别名
        writer.addHeaderAlias("id", "编号");
        writer.addHeaderAlias("username", "用户名");
        writer.addHeaderAlias("nickname", "昵称");
        writer.addHeaderAlias("type", "类型");
        writer.addHeaderAlias("email", "邮箱");
        writer.addHeaderAlias("phone", "手机号");
        writer.addHeaderAlias("sex", "性别");
        writer.addHeaderAlias("status", "状态");
        writer.addHeaderAlias("loginIp", "最后登录IP");
        writer.addHeaderAlias("loginTime", "最后登录时间");
        // 默认的，未添加alias的属性也会写出，如果想只写出加了别名的字段，可以调用此方法排除之
        writer.setOnlyAlias(true);
        // 一次性写出内容，使用默认样式，强制输出标题
        writer.write(rows, true);
        // 写出到客户端下载
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=export_" + System.nanoTime() + ".xls");
        ServletOutputStream servletOutputStream = response.getOutputStream();
        writer.flush(servletOutputStream, true);
        writer.close();
        servletOutputStream.close();
    }

}
